package cn.yi.fast.security.shiro;

import com.google.common.collect.Maps;
import lombok.Setter;
import org.apache.shiro.cache.AbstractCacheManager;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;
import java.util.Objects;

public class RedisCacheManager extends AbstractCacheManager {
    @Setter
    private RedisTemplate redisTemplate;
    private Map<String, Cache> caches = Maps.newConcurrentMap();
    @Setter
    private Long timeout;

    @Override
    protected Cache createCache(String s) throws CacheException {
        Cache cache = caches.get(s);
        if (Objects.isNull(cache)) {
            cache = new RedisCache(redisTemplate, s, timeout);
        }
        caches.put(s, cache);
        return cache;
    }
}
